package com.baizhi.controller;

import com.baizhi.entity.Result;
import com.baizhi.entity.UserFigure;
import com.baizhi.service.UserFigureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("userFigure")
@Controller
public class UserFigureController {

    @Autowired
    private UserFigureService userFigureService;

    @RequestMapping("findAll")
    @ResponseBody
    public List<UserFigure> findAll(){
       return userFigureService.findAll();
    }

    @ResponseBody
    @RequestMapping("findByUserId")
    public List<UserFigure> findByUserId(String id){
        return userFigureService.findByUserId(id);
    }

    @ResponseBody
    @RequestMapping("findById")
    public UserFigure findById(String id){
        return userFigureService.findById(id);
    }

    @ResponseBody
    @RequestMapping("save")
    public Result save(UserFigure userFigure){
        Result result = null;
        try{
            userFigureService.save(userFigure);
            result = new Result();
            result.setStatus(true);
        }catch (Exception e){
            e.printStackTrace();
            result = new Result();
            result.setMessage("添加失败");
            throw new RuntimeException("添加失败");
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("update")
    public Result update(UserFigure userFigure){
        Result result = null;
        try{
            userFigureService.update(userFigure);
            result = new Result();
            result.setStatus(true);
        }catch (Exception e){
            e.printStackTrace();
            result = new Result();
            result.setMessage("更新失败");
            throw new RuntimeException("更新失败");
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("delete")
    public Result delete(String id){
        Result result = null;
        try{
            userFigureService.delete(id);
            result = new Result();
            result.setStatus(true);
        }catch (Exception e){
            e.printStackTrace();
            result = new Result();
            result.setMessage("删除失败");
            throw new RuntimeException("删除失败");
        }
        return result;
    }
}
